package com.tangruitao.service.impl;

import com.tangruitao.bean.*;
import com.tangruitao.dao.OrderDao;
import com.tangruitao.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 这个类的作者是13536
 * 开发日期:2021/12/30
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;
    @Override
    public Users SelectUserByName(String name) {
        return orderDao.SelectUserByName(name);
    }

    @Override
    public List<Order> selectOrder(QueryOv ov) {
        return orderDao.selectOrder(ov);
    }

    @Override
    public int insertOrder(Order order) {
//        增加主表
      int i=  orderDao.insertOrder(order);
        System.out.println("999999999999999999"+order);
//      增加中间表
        List<Detail> details = order.getDetails();
        System.out.println("111111111111111111"+details);
        for (Detail detail : details) {
            orderDao.insertDetail(detail);
            orderDao.insetZh(order.getOrderId(),detail.getDetailId());
//            增加明细表
        }

        return i;
    }

    @Override
    public Detail selectDetailByNnm(String num) {

        return orderDao.selectDetailByNnm(num);
    }

    @Override
    public int deleteOrder(int id) {
        int i=0;
        List<Zhong> zhongs = orderDao.selectZhByid(id);
        for (Zhong zhong : zhongs) {
             i+= orderDao.deletDetail(zhong.getDetailId());
        }
        i+=orderDao.delectzh(id);
        i+=orderDao.deleteOrder(id);
        return i;
    }

    @Override
    public List<Node> selectNote() {
        List<Node> nodes = orderDao.selectNote();
        System.out.println("111111111111111111111"+nodes);
//        List<Node> collect = nodes.stream().filter(a -> Objects.equals(a.getPid(), pid)).collect(Collectors.toList());
        nodes.forEach(a->a.setNodes(getselectNote(nodes,a.getNodeId())));
        return nodes;
    }
    public List<Node> getselectNote(List<Node> nodes,int pid){
        List<Node> collect = nodes.stream().filter(a -> Objects.equals(a.getPid(), pid)).collect(Collectors.toList());
        System.out.println("222222222222222222222222222222"+collect);
        collect.forEach(a->a.setNodes(getselectNote(nodes,a.getNodeId())));
        return collect;
    }

}
